<?php
class MemberController extends AdminController{
    public function listAction(){
        $model = new MemberModel;
        $member = $model->select();
        $this->_view->member = $member;
    }
    public function addAction(){
        $model = new UserRoleModel;
        $role = $model->select();
        $this->_view->role = $role;
    }
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $model = new MemberModel;
        $data['name'] = Request::aspost(6,'name');
        $password = Request::aspost(6,'password');
        if(empty($password)){
            Response::error('Lost the Password');
            return false;
        }else{
            // Mark
            $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
            $options = [
                'cost' => 11,
                'salt' => $salt,
            ];
            $data['status'] = 1;
            $data['password'] = password_hash($password,PASSWORD_BCRYPT,$options);
        }
        $flag = $model->insert($data);
        if($flag == 0 || $flag == false){
            Response::error('The user name was used');
        }else{
            Response::success('nice');
        }
    }

    public function editAction(){
        $id = Request::asget(3,'id',null);
        if(null === $id){
            die;
        }else{
            $model = new MemberModel;
            $member = $model->where(['id'=>$id])->one();
            $this->_view->member = $member;
        }
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $model = new MemberModel;
        $data['name'] = Request::aspost(6,'name',null);
        $password = Request::aspost(6,'password',null);
        $id = Request::aspost(3,'id',null);
        if($password != null){
            $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
            $options = [
                'cost' => 11,
                'salt' => $salt,
            ];
            $data['status'] = 1;
            $data['password'] = password_hash($password,PASSWORD_BCRYPT,$options);
        }
        $flag = $model->where(['id'=>$id])->update($data);
        if($flag == 0 || $flag == false){
            Response::error('The user name was used');
        }else{
            Response::success('nice');
        }
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            $status = isset($_POST['status']) ? $_POST['status'] : '';
            if($status == ''){
                return false;
            }else{
                switch($status){
                case '0': $data['status'] = 1;
                    break;
                case '1': $data['status'] = 0;
                    break;
                }
                $model = new MemberModel;
                $flag = $model->where(['id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('');
                }else{
                    Response::success('nice');
                }
            }
        }
    }
    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if(null == $id){
            die;
        }else{
            $model = new MemberModel;
            $flag = $model->where(['id' => $id])->delete();
            if($flag == 0){
                return Response::success('nice');
            }else{
                return Response::error('error');
            }            
        }

    }
   
}
?>
